From e143e0857599dfb6c5f3ed9c50ba01683caa0999 Mon Sep 17 00:00:00 2001 From: robertl Date: Thu, 19 Nov 2009 03:56:03 +0000 Subject: [PATCH] Add serial port detection to Windows. git-svn-id: http://gpsbabel.googlecode.com/svn/trunk@3823 f51c46e8-681c-474f-0cfe-069cfd0219fb --- gpsbabel/gui/serial_win.cpp | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/gpsbabel/gui/serial_win.cpp b/gpsbabel/gui/serial_win.cpp index bd569541f..67101b13d 100644 --- a/gpsbabel/gui/serial_win.cpp +++ b/gpsbabel/gui/serial_win.cpp @@ -1,4 +1,4 @@ -// $Id: serial_win.cpp,v 1.1 2009-09-02 19:05:27 robertl Exp $ +// $Id: serial_win.cpp,v 1.2 2009-11-19 03:56:03 robertl Exp $ //------------------------------------------------------------------------ // // Copyright (C) 2009 S. Khai Mong . @@ -21,6 +21,8 @@ #include "mainwindow.h" +#if 0 // Does not require Windows 2000 + static const char *deviceNames[] = { "com1:", "com2:", @@ -35,3 +37,35 @@ void MainWindow::osLoadDeviceNameCombos(QComboBox *box) box->addItem(deviceNames[i]); } } + +#else // This code assumes Windows 2000 or later + +// Uses QueryDosDevice(), Minimum supported: Windows 2000 Professional/Server +#include +#include + +void MainWindow::osLoadDeviceNameCombos(QComboBox *box) +{ + char DevList[64*1024-1]; // a single byte more, and certain versions of windows + // always return QueryDosDevice()==0 && GetLastError()==ERROR_MORE_DATA. + // see http://support.microsoft.com/kb/931305 + // Get a list of all existing MS-DOS device names. Stores one or more asciiz strings followed by an extra null. + DWORD res = QueryDosDeviceA(NULL, DevList, sizeof(DevList)); + if (res == 0) + { + DWORD err = GetLastError(); // could check for ERROR_INSUFFICIENT_BUFFER, and retry with a larger buffer. + // but DevList is already at the maximum size it can be without running into kb 931305. + // FIXME: This shold be a QMessageBox::warning() - RJL + // fprintf(stderr,"QueryDosDevice() failed with %d. GetLastError()==%d.\n", res, err); + return; + } + + for (char *p=DevList; *p;) { + int len = strlen(p); + if (strncmp(p,"COM",3)==0) + box->addItem((PCHAR)p); + p += len+1; // +1 to also skip the null character of each string + } +} + +#endif -- 2.30.2